-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Added Asynchronous_requests.ipynb notebook for JavaScript #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Added Asynchronous_requests.ipynb notebook for JavaScript #1014
Conversation
Aarchi-07
commented
Oct 29, 2025
- Converted Python notebook to JavaScript.
- Preserved original logic and functionality.
- Optimized code structure for clarity and maintainability.
Summary of ChangesHello @Aarchi-07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request adds a new JavaScript notebook to the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a new JavaScript notebook for demonstrating asynchronous requests with the Gemini API. The conversion from the Python notebook is a good start, but there are several critical issues that need to be addressed.
First, the filename Asynchronous Python requests.txt and the notebook title incorrectly refer to "Python" instead of "JavaScript". This should be corrected to avoid confusion.
Second, and more importantly, many variables throughout the file are used without being declared with const, let, or var. This creates global variables, which is a major anti-pattern in JavaScript that can lead to bugs and unexpected behavior. I've left several comments pointing these out.
Third, the repository style guide (line 8) requires new notebooks to be referenced in the corresponding README files. This PR does not seem to update quickstarts-js/README.md to include the new notebook.
Please review my detailed comments to fix these issues and improve the code quality and correctness.
| */ | ||
|
|
||
| /* Markdown (render) | ||
| # Gemini API: Asynchronous Python requests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| module = await import("https://esm.sh/@google/[email protected]"); | ||
| GoogleGenAI = module.GoogleGenAI; | ||
| ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); | ||
|
|
||
| MODEL_ID = "gemini-2.5-flash" // ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables module, GoogleGenAI, ai, and MODEL_ID are not declared with const, let, or var. This creates global variables, which is a bad practice and can lead to unexpected behavior. Please declare them with const as they are not reassigned.
const module = await import("https://esm.sh/@google/[email protected]");
const GoogleGenAI = module.GoogleGenAI;
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const MODEL_ID = "gemini-2.5-flash" // ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"]
| prompt = "Describe this image in just 3 words."; | ||
|
|
||
| imgFilenames = ["firefighter.jpg", "elephants.jpeg", "jetpack.jpg"]; | ||
| imgDir = "https://storage.googleapis.com/generativeai-downloads/images/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables prompt, imgFilenames, and imgDir are not declared with const, let, or var. This creates global variables, which is a bad practice. Please declare them with const as they are not reassigned.
const prompt = "Describe this image in just 3 words.";
const imgFilenames = ["firefighter.jpg", "elephants.jpeg", "jetpack.jpg"];
const imgDir = "https://storage.googleapis.com/generativeai-downloads/images/";
| imageList = [] | ||
| for (imgFilename of imgFilenames) { | ||
| imageBlob = await fetch(imgDir + imgFilename).then(res => res.blob()); | ||
| imageBase64 = await new Promise((resolve) => { | ||
| reader = new FileReader(); | ||
| reader.onloadend = () => resolve(reader.result.split(',')[1]); | ||
| reader.readAsDataURL(imageBlob); | ||
| }); | ||
|
|
||
| imageList.push({ inlineData: { data: imageBase64, mimeType: "image/jpeg" } }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several undeclared variables in this block (imageList, imgFilename, imageBlob, imageBase64, reader). Using variables without declaring them with const, let, or var creates global variables, which is a bad practice and can lead to bugs. Please declare them with const or let.
const imageList = []
for (const imgFilename of imgFilenames) {
const imageBlob = await fetch(imgDir + imgFilename).then(res => res.blob());
const imageBase64 = await new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(imageBlob);
});
imageList.push({ inlineData: { data: imageBase64, mimeType: "image/jpeg" } });
}
| async function describeLocalImages() { | ||
| for (image of imageList) { | ||
| response = await ai.models.generateContent({ | ||
| model: MODEL_ID, | ||
| contents: [prompt, image], | ||
| }); | ||
| console.log(response.text); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variables image and response inside the describeLocalImages function are not declared with const, let, or var. This creates global variables, which is a bad practice. Please declare them within their scope.
async function describeLocalImages() {
for (const image of imageList) {
const response = await ai.models.generateContent({
model: MODEL_ID,
contents: [prompt, image],
});
console.log(response.text);
}
}
| async function downloadImage(imgUrl) { | ||
| imageBlob = await fetch(imgUrl).then(res => res.blob()); | ||
| imageBase64 = await new Promise((resolve) => { | ||
| const reader = new FileReader(); | ||
| reader.onloadend = () => resolve(reader.result.split(',')[1]); | ||
| reader.readAsDataURL(imageBlob); | ||
| }); | ||
|
|
||
| return { inlineData: { data: imageBase64, mimeType: "image/jpeg" } }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variables imageBlob and imageBase64 inside the downloadImage function are not declared with const, let, or var. This creates global variables, which is a bad practice. Please declare them with const.
async function downloadImage(imgUrl) {
const imageBlob = await fetch(imgUrl).then(res => res.blob());
const imageBase64 = await new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(imageBlob);
});
return { inlineData: { data: imageBase64, mimeType: "image/jpeg" } };
}
| async function processImage(imgPromise) { | ||
| response = await ai.models.generateContent({ | ||
| model: MODEL_ID, | ||
| contents: [prompt, await imgPromise], | ||
| }); | ||
| return response.text; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The response variable inside the processImage function is not declared with const, let, or var. This creates a global variable, which is a bad practice. Please declare it with const.
async function processImage(imgPromise) {
const response = await ai.models.generateContent({
model: MODEL_ID,
contents: [prompt, await imgPromise],
});
return response.text;
}
| responsePromises = []; | ||
|
|
||
| for (imgFilename of imgFilenames) { | ||
| const imgPromise = downloadImage(imgDir + imgFilename); | ||
| const textPromise = processImage(imgPromise); | ||
| responsePromises.push(textPromise); | ||
| } | ||
|
|
||
| console.log(`Download and content generation queued for ${responsePromises.length} images.`); | ||
|
|
||
| for await (response of responsePromises) { | ||
| console.log(await response); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few issues in this block:
- Variables
responsePromises,imgFilename, andresponseare not declared withconst,let, orvar. This creates global variables, which is a bad practice. - On line 149,
await responseis unnecessary. Thefor await...ofloop already awaits each promise fromresponsePromises, soresponseis a resolved string, not a promise. Awaiting a non-promise value is a no-op but it is confusing and should be removed.
Please declare the variables and remove the redundant await.
const responsePromises = [];
for (const imgFilename of imgFilenames) {
const imgPromise = downloadImage(imgDir + imgFilename);
const textPromise = processImage(imgPromise);
responsePromises.push(textPromise);
}
console.log(`Download and content generation queued for ${responsePromises.length} images.`);
for await (const response of responsePromises) {
console.log(response);
}
| imgFilenames = ["firefighter.jpg", "elephants.jpeg", "jetpack.jpg"]; | ||
| imgDir = "https://storage.googleapis.com/generativeai-downloads/images/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Hi @Aarchi-07 , Could you take a look and resolve the code comments added by the bot? Thank You!! |
|
@shmishra99 @Aarchi-07 The comments regarding const, let, and var cannot be resolved at this time because the applet does not maintain multi-cell context for variables declared using these keywords. Therefore, variables should be declared without those. |
|
Marking this pull request as stale since it has been open for 14 days with no activity. This PR will be closed if no further activity occurs. |